package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author lianming
 * @email sun_moon2022@sina.com
 * @date 2022-05-23 18:26:53
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    //attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addRealtion(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatchRelations(vos);
        return R.ok();
    }

    //product/attrgroup/{catelogId}/withattr
    //查找所有分类，即分类下的所有属性
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWhithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWhithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    ///product/attrgroup/{attrgroupId}/attr/relation
    //分组关联的属性
    @GetMapping("/{attrgroupId}/attr/relation")
    public R groupAllAttrs(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrService.getGroupAllAttrs(attrgroupId);
        return R.ok().put("data", list);
    }
    //    attrgroup/2/noattr/relation
    //    //与当前分组同一个catelogid的属性，且当前分组没有关联
    //分组没有关联的属性
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R groupAllNoAttrs(@RequestParam Map<String, Object> params,
                             @PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getGroupAllNoAttrs(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
		//将当前组所在三级分类的完整路径放进attrGroup
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
//        attrGroupService.removeByIds(Arrays.asList(vos));
        //批量删除
        attrGroupService.DeleteRelations(vos);
        return R.ok();
    }






}
